#include <Arduino.h>

#define RED_PIN   25  // 红色通道的GPIO引脚
#define GREEN_PIN 26  // 绿色通道的GPIO引脚
#define BLUE_PIN  27  // 蓝色通道的GPIO引脚

#define CHANNEL_RED   0
#define CHANNEL_GREEN 1
#define CHANNEL_BLUE  2

#define FREQ_PWM 5000  // PWM频率设定为5000Hz
#define RESOLUTION 8   // PWM分辨率设置为8位

void setup() {
  Serial.begin(115200); // 初始化串口通信，波特率115200

  // 设置PWM通道
  ledcSetup(CHANNEL_RED, FREQ_PWM, RESOLUTION);
  ledcSetup(CHANNEL_GREEN, FREQ_PWM, RESOLUTION);
  ledcSetup(CHANNEL_BLUE, FREQ_PWM, RESOLUTION);

  // 关联通道与GPIO引脚
  ledcAttachPin(RED_PIN, CHANNEL_RED);
  ledcAttachPin(GREEN_PIN, CHANNEL_GREEN);
  ledcAttachPin(BLUE_PIN, CHANNEL_BLUE);
}

void setColor(uint8_t redValue, uint8_t greenValue, uint8_t blueValue, String colorName) {
  // 通过乘以一个小于1的因子来降低亮度
  float brightness = 0.5; // 调整亮度, 0.0~1.0
  ledcWrite(CHANNEL_RED, redValue * brightness);
  ledcWrite(CHANNEL_GREEN, greenValue * brightness);
  ledcWrite(CHANNEL_BLUE, blueValue * brightness);
  Serial.println("LED显示" + colorName); // 串口输出当前显示的颜色名称
  delay(1000); // 持续显示1秒
}


void loop() {
  setColor(255, 0, 0, "红色");        // 红色
  setColor(255, 125, 0, "橙色");      // 橙色
  setColor(255, 255, 0, "黄色");      // 黄色
  setColor(0, 255, 0, "绿色");        // 绿色
  setColor(0, 255, 255, "青色");      // 青色
  setColor(0, 0, 255, "蓝色");        // 蓝色
  setColor(255, 0, 255, "紫色");      // 紫色
}
